package android.gpsmessenger;

import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class RecordFrequencyFragment extends ListFragment 
{
    private int itemSelected = -1; 
    private String[] frequencies = {"2 sec", "30 sec", "1 min", "2 min", 
    		"5 min", "10 min", "15 min", "30 min", "60 min"};
    
    public void onResume() 
    {
		super.onResume();
		setListAdapter(getRecordFrequencies());
    }

    public void onPause() 
    {
    		super.onPause();
    }
    
	public void onCreate (Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setHasOptionsMenu(true);
	}
	
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
	{
		inflater.inflate(R.menu.fragment_ok_cancel_menu, menu);
	}
	
	public boolean onOptionsItemSelected (MenuItem item) 
	{
        Intent intent = new Intent(getActivity(), CurrentTripActivity.class);
		if (item.getItemId() == R.id.menu_ok) 
		{
			TextView text = (TextView) getListView().getChildAt(itemSelected);
			intent.putExtra("frequency", text.getText().toString());
	        getActivity().startActivity(intent);
	        itemSelected = -1;
    		getActivity().invalidateOptionsMenu();
		}
		if (item.getItemId() == R.id.menu_cancel) 
		{
			getActivity().finish();			
		}
		return true;
	}
	
    public void onListItemClick(ListView l, View v, int position, long id) 
    {
    		itemSelected = position;
    		getActivity().invalidateOptionsMenu();
    }
	
	public void onActivityCreated(Bundle savedInstanceState) 
	{
		super.onActivityCreated(savedInstanceState);
		setListAdapter(getRecordFrequencies());
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
	}
	
    public ArrayAdapter<String> getRecordFrequencies() 
    {
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice);
                
        for (String f : frequencies) 
        {
        	adapter.add(f);
        }

        return adapter;
    }
}
